﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTCategoryNewsUI : System.Web.UI.Page
    {

        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private Int32 DanhMucID = HangSo.DEFAULT_ID;
        private String message = "";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dtProductCategorys = DanhMucTinTucProvider.GetDanhMucTinTuc().Tables[0];
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    DanhMucID = Int32.Parse(Request.QueryString["DanhMucID"]);
                }
                catch
                {

                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadLoaiDanhMuc();

                CreateDDLCategory(0, 0);
                if (IsVN == true)
                    this.ddlDanhMucCha.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                else
                    this.ddlDanhMucCha.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                if (DanhMucID != -1)
                {
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Edit_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Edit_Other;
                    DanhMucTinTuc theDanhMucTin = DanhMucTinTucProvider.GetDanhMucTinTucByDanhMucID(DanhMucID);
                    txtenDanhMuc.Text = theDanhMucTin.TenDanhMucTinTucVN;
                    txtTenDanhMucTinTucOther.Text = theDanhMucTin.TenDanhMucTinTucOther;
                    ddlDanhMucCha.SelectedValue = theDanhMucTin.ChuyenMucChaID.ToString();
                    try
                    {
                        ddlLoaiDanhMuc.SelectedValue = theDanhMucTin.TypeDanhMuc.ToString();
                    }
                    catch
                    {
                        ddlLoaiDanhMuc.SelectedValue = "-1";
                    }
                    ViewState["DanhMucID"] = DanhMucID.ToString();
                }
                else
                {
                    ViewState["action"] = HangSo.ACTION_ADD;
                   if(IsVN==true)
                       lbAdd_Edit.Text = HangSo.Add_VN;
                    else
                       lbAdd_Edit.Text = HangSo.App_Other;

                }
               
            }
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbApply.Text = rm.GetString("lbApply", ci);
            lbCategoryNews.Text = rm.GetString("lbCategoryNews", ci);
            lbDanhMucCha.Text = rm.GetString("lbDanhMucCha", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbSave.Text = rm.GetString("lbSave", ci);
            lbTenDanhMuc.Text = rm.GetString("lbTenDanhMuc", ci);
            lbTenDanhMucTinTucOther.Text = rm.GetString("lbTenDanhMucTinTucOther", ci);
            lbThuTu.Text = rm.GetString("lbThuTu", ci);
            lbLoaiDanhMuc.Text = rm.GetString("lbLoaiDanhMuc", ci);   
        }

        public Boolean ValidateData()
        {
            Boolean flag = true;
            if (txtenDanhMuc.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            //if (txtTenDanhMucTinTucOther.Text == "")
            //{
            //    if (IsVN == true)
            //        message = HangSo.check_VN;
            //    else
            //        message = HangSo.check_Other;
            //    flag = false;
            //}
            return flag;
        }


        public void LoadLoaiDanhMuc()
        {
            if (IsVN == true)
            {
                this.ddlLoaiDanhMuc.Items.Insert(0, new ListItem(HangSo.TinTuc, "-1"));
                this.ddlLoaiDanhMuc.Items.Insert(1, new ListItem(HangSo.VanChuyen, "1"));
                this.ddlLoaiDanhMuc.Items.Insert(2, new ListItem(HangSo.TuyenDung, "2"));
                this.ddlLoaiDanhMuc.Items.Insert(3, new ListItem(HangSo.DoiTac, "3"));
            }
            else
            {
                this.ddlLoaiDanhMuc.Items.Insert(0, new ListItem(HangSo.TinTucOther, "-1"));
                this.ddlLoaiDanhMuc.Items.Insert(1, new ListItem(HangSo.VanChuyenOther, "1"));
                this.ddlLoaiDanhMuc.Items.Insert(2, new ListItem(HangSo.TuyenDungOther, "2"));
                this.ddlLoaiDanhMuc.Items.Insert(3, new ListItem(HangSo.DoiTacOther, "3"));
            }
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucTinTucID"].ToString());
                ListItem li;
                if(IsVN==true)
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucVN"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucOther"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                this.ddlDanhMucCha.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            string action = ViewState["action"].ToString();
            Int32 DanhMucTinTucIDOut = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                DanhMucTinTuc theDanhMuc = new DanhMucTinTuc();
                theDanhMuc.TenDanhMucTinTucVN = txtenDanhMuc.Text;
                theDanhMuc.TenDanhMucTinTucOther = txtTenDanhMucTinTucOther.Text;
                
                try
                {
                    theDanhMuc.ChuyenMucChaID = Int32.Parse(ddlDanhMucCha.SelectedValue);
                    theDanhMuc.SapXep = Int32.Parse(txtThuTu.Text);
                }
                catch
                {
                    theDanhMuc.ChuyenMucChaID = 0;
                    theDanhMuc.SapXep = 1;
                }

                if (action == HangSo.ACTION_ADD)
                {
                    theDanhMuc.DanhMucTinTucID = -1;
                    DanhMucTinTucIDOut = DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMuc, true);
                }
                else
                {
                    theDanhMuc.DanhMucTinTucID = Int32.Parse(ViewState["DanhMucID"].ToString());
                    DanhMucTinTucIDOut = DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMuc, false);
                }
            }
            Response.Redirect("EHTCategoryNews.aspx");
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            string action = ViewState["action"].ToString();
            Int32 DanhMucTinTucIDOut = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                DanhMucTinTuc theDanhMuc = new DanhMucTinTuc();
                theDanhMuc.TenDanhMucTinTucVN = txtenDanhMuc.Text;
                theDanhMuc.TenDanhMucTinTucOther = txtTenDanhMucTinTucOther.Text;                

                try
                {
                    theDanhMuc.ChuyenMucChaID = Int32.Parse(ddlDanhMucCha.SelectedValue);
                    theDanhMuc.SapXep = Int32.Parse(txtThuTu.Text);
                }
                catch
                {
                    theDanhMuc.ChuyenMucChaID = 0;
                    theDanhMuc.SapXep = 1;
                }

                if (action == HangSo.ACTION_ADD)
                {
                    theDanhMuc.DanhMucTinTucID = -1;
                    DanhMucTinTucIDOut = DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMuc, true);
                }
                else
                {
                    theDanhMuc.DanhMucTinTucID = Int32.Parse(ViewState["DanhMucID"].ToString());
                    DanhMucTinTucIDOut = DanhMucTinTucProvider.IUDanhMucTinTuc(theDanhMuc, false);
                }
            }
            String str = "";
            if (IsVN == true)
                str = HangSo.App_VN;
            else
                str = HangSo.App_Other;
            Response.Redirect("EHTCategoryNewsUI.aspx?DanhMucID=" + DanhMucTinTucIDOut + "&&Message" + str);
        }
    }
}